/*
 * @Author: lin_zizhuo 2297304135@qq.com
 * @Date: 2023-11-20 14:37:27
 * @LastEditors: lin_zizhuo 2297304135@qq.com
 * @LastEditTime: 2023-11-20 15:58:36
 * @FilePath: /named_pipe/log.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#pragma once

#include <unistd.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <cstring>
#include <cerrno>
#include <cstdio>
#include <string>
#include <iostream>

const int mesg_size = 1024;
const std::string log_file = "./log/log.txt";

enum record_option
{
    screen,
    one_file,
    diverse_file
};

enum Level
{
    Info,
    Debug,
    Warning,
    Error,
    Fatal
};

class Log
{
public:
    // 使用指定方法记录日志
    void change_record_op(record_option op)
    {
        _op = op;
    }
    void operator()(Level l, const char *format, ...)
    {
        char right_buffer[mesg_size], left_buffer[mesg_size];
        va_list parameter;
        va_start(parameter, format);
        vsnprintf(right_buffer, mesg_size, format, parameter);
        time_t now_time = 0;
        time(&now_time);
        struct tm *time_st = localtime(&now_time);
        snprintf(left_buffer, mesg_size, "[%s] [%d-%d-%d %d:%d:%d] @ ", level_to_string(l).c_str(), time_st->tm_year + 1900, time_st->tm_mon + 1, time_st->tm_mday, time_st->tm_hour, time_st->tm_min, time_st->tm_sec);
        std::string mesg;
        mesg += left_buffer;
        mesg += right_buffer;
        mesg += '\n';
        record_log(l, mesg);
    }

private:
    record_option _op = screen;

    void write_to_file(Level l, const std::string &mesg, const std::string &file_name)
    {
        int fd = open(file_name.c_str(), O_WRONLY | O_CREAT | O_APPEND, 0666);
        if (fd < 0)
        {
            perror("open");
            exit(1);
        }
        write(fd, mesg.c_str(), mesg.size());
        close(fd);
    }
    void record_log(Level l, const std::string &mesg)
    {
        if (_op == screen)
            std::cout << mesg;
        else if (_op == one_file)
            // 都是进行文件写入的操作
            write_to_file(l, mesg, log_file);
        else
            write_to_file(l, mesg, log_file + "." + level_to_string(l));
    }
    const std::string level_to_string(Level l)
    {
        switch (l)
        {
        case Info:
            return "Info";
            break;
        case Debug:
            return "Debug";
            break;
        case Warning:
            return "Warning";
            break;
        case Error:
            return "Error";
            break;
        case Fatal:
            return "Fatal";
            break;
        default:
            return "None";
            break;
        }
    }
};

Log lg;
// C语言在压栈的时候是从右向左压栈的
// C语言模板参数包使用方法
// int sum(int n, ...)
// {
//     va_list s;
//     // 传入n，通过n找到可变参数包的最后一个参数
//     va_start(s, n);
//     int sum = 0;
//     while (n)
//     {
//         sum += va_arg(s, int);
//         n--;
//     }
//     va_end(s);
//     return sum;
// }